package com.czbank.financing.controller;

import com.alibaba.fastjson.JSON;
import com.czbank.financing.Entity.UserInfo;
import com.czbank.financing.service.LoginService;
import com.czbank.financing.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: thinkpad
 * @Date: 2019/7/11 10:17
 * @Description:
 */
@RestController
@CrossOrigin
public class LoginController {
    @Autowired
    private LoginService loginService;
    @Autowired
    private TokenService tokenService;

/*    @RequestMapping("/login")
    public String login( Model model,
     UserInfo userInfo){
        boolean existCardNo=loginService.existAccount(userInfo.getAccount());
        System.out.println("userInfo.getAccount() = " + userInfo.getAccount());
        userInfo = loginService.Login(userInfo.getAccount(),userInfo.getPassword() );
        if (existCardNo){
            userInfo = loginService.Login(userInfo.getAccount(),userInfo.getPassword() );
            if (userInfo==null){
                model.addAttribute("errorMsg", "密码错误");
                return "user/login";
            }
            else {

                return "user/main";
            }
        }else {
            model.addAttribute("errorMsg", "卡号不存在");
            return "user/login";
        }

    }*/
/*    @RequestMapping("/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password,HttpSession session){
        UserInfo userInfo = loginService.Login(account,password);
        if (userInfo==null){
            return "user/main";
        }else {
            return "user/main2";
        }
    }*/

/*    @GetMapping("/login")
    public String logindata(Model model){
        model.addAttribute("userInfo", new UserInfo());
        return "user/Userlogin";

    }

    @PostMapping("/login")
    public String loginsun(@ModelAttribute UserInfo userInfo){
        userInfo = loginService.Login(userInfo.getAccount(),userInfo.getPassword());
        if (userInfo==null){
            return "user/main2";
        }else {
            return "user/main";
        }


    }*/

 /* @RequestMapping("/login")
    public Object login(String account,
                        String password){
        ModelAndView modelAndView = new ModelAndView();
        boolean existCardNo=loginService.existAccount(account);
        if (existCardNo){
            modelAndView.setViewName("login");
            return "success";
        }
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());

        UserInfo userInfo=loginService.Login(account,md5Password);
        modelAndView.setViewName("user/index");
        return "sss";
    }*/

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object login(@RequestBody UserInfo userInfo) throws UnsupportedEncodingException {


        String password = userInfo.getPassword();
        System.out.println("password = " + password);
        String account = userInfo.getAccount();
        System.out.println("account = " + account);
        boolean existCardNo = loginService.existAccount(account);

        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());

        userInfo = loginService.Login(account, md5Password);
        System.out.println("userInfo = " + userInfo);


        String token = tokenService.getToken(userInfo);
        Map<Object,Object> hashmap = new HashMap<>();
        hashmap.put("user",userInfo );
        hashmap.put("token",token );

        System.out.println("hashmap = " + JSON.toJSONString(hashmap));

        return JSON.toJSONString(hashmap);

    }

    //    @RequestMapping(value = "/devlogin",method = RequestMethod.POST)
//    public Object DevLogin(@RequestBody UserInfo userInfo){
//
//        String password=userInfo.getPassword();
//        System.out.println("password = " + password);
//        String account=userInfo.getAccount();
//        System.out.println("account = " + account);
//
//        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
//        userInfo=loginService.Login(account,md5Password);
//        System.out.println("userInfo = " + userInfo);
//        return   JSON.toJSONString(userInfo);
//
//    }
    @RequestMapping(value = "/devlogin", method = RequestMethod.POST)
    public Object DevLogin(@RequestBody Map<Object,String> map) {

        System.out.println("map = " + map);
        return "sad";

    }

    @RequestMapping("/Index")
    public Object ftlIndex() {

        return "user/index";
    }


}


